package icon

import (
	"context"
	"feishup2pclient/msg"
	"fmt"

	"github.com/getlantern/systray"
	"github.com/wailsapp/wails/v2/pkg/runtime"
)

func Icon_main(ctx context.Context) {
	onExit := func() {
		// now := time.Now()
		runtime.Quit(ctx)
		// ioutil.WriteFile(fmt.Sprintf(`on_exit_%d.txt`, now.UnixNano()), []byte(now.String()), 0644)
	}
	onReady := func() {
		systray.SetTemplateIcon(Data, Data)
		systray.SetTitle("飞鼠")
		systray.SetTooltip("飞鼠")
		mOpenOrig := systray.AddMenuItem("打开", "打开app")
		mHideOrig := systray.AddMenuItem("关于 飞鼠科技", "飞鼠科技")
		mQuitOrig := systray.AddMenuItem("退出", "退出app")
		go func() {
			for {
				select {
				case <-mQuitOrig.ClickedCh:
					fmt.Println("Requesting quit")
					systray.Quit()
					fmt.Println("Finished quitting")
				case <-mHideOrig.ClickedCh:
					runtime.Show(ctx)
					msg.ConfigCheck("config")
					// if run.GOOS == "windows" {
					// 	cmd := exec.Command(`cmd`, `/c`, `start`, `https://www.feishuwg.com/`)
					// 	cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
					// 	cmd.Start()
					// } else if run.GOOS == "darwin" {
					// 	exec.Command(`open`, `https://www.feishuwg.com/`).Start()
					// }
				case <-mOpenOrig.ClickedCh:
					msg.ConfigCheck("home")
					runtime.Show(ctx)
					runtime.WindowSetAlwaysOnTop(ctx, true)
					runtime.WindowSetAlwaysOnTop(ctx, false)
				}
			}

		}()
	}
	systray.Run(onReady, onExit)
}
